import { Component, OnInit } from '@angular/core';
import { Response} from '@angular/http';
import { User} from "../model/user-model";
import { GlobalService} from "../../global.service";
import { UserInfoService} from "./user-info.service";

@Component({
  selector: 'app-user-info',
  templateUrl: './user-info.component.html',
  styleUrls: ['./user-info.component.css']
})
export class UserInfoComponent implements OnInit {

  user:User;
  editNameFlag:boolean=false;
  bindStudentCode:string;
  bindStudentName:string;
  bindCheckCode:string;
  studentCode:string;

  constructor(
    private globalService:GlobalService,
    private userInfoService:UserInfoService,
  ) { }

  ngOnInit() {
    this.user = this.globalService.getCurrentUser();
    this.studentCode = this.user.studentNumber;
    console.log(this.user);
  }

  updateName(){
    this.editNameFlag = true;
  }

  updateNameSubmit(){
    console.log("发送更改名称请求");
    let sendData = {
      code:this.user.code,
      name:this.user.name,
      properties:new Properties()
    };
    sendData.properties.updateName = this.user.name;
    this.userInfoService.save(sendData).subscribe((res:Response)=>{
      console.log(res.json());
    });
    this.editNameFlag = false;
  }

  bind(){

    let sendData = {
      code:this.user.code,
      password:this.bindCheckCode,
      name:this.bindStudentName,
      studentNumber:this.bindStudentCode,
    };

    this.userInfoService.bindStudentNumber(sendData).subscribe((res=>{
      console.log(res.json());
      if(res.json().success){
        this.studentCode = res.json().value.studentNumber;
      }
    }));
  }
}
class Properties{
  updateName:any;
}
